<?php


namespace common\services;

use common\models\com\ComProducts;
use common\models\es\EsComProducts;

class EsCloudProduct
{
    /**
     * 同步com_products数据到es
     * @param array|int $id
     * @return bool
     */
    public function syncProduct($id)
    {
        if (!$id) {
            return false;
        }
        $id = is_array($id) ? $id : [$id];
        $products = [];
        $models = ComProducts::find()->where(['id' => $id])->all();
        $es = new EsComProducts();
        $attributes = $es->attributes();
        foreach ($models as $model) {
            $product = [];
            foreach ($attributes as $column) {
                $product[$column] = $model->$column;
            }
            $products[] = $product;
        }
        $res = $products ? $es->syncData($products) : [];
        if (isset($res['errors']) && $res['errors'] == false) {
            return true;
        }
        return false;
    }
}